package com.daisuxia.back.controller;

import java.util.Date;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.daisuxia.back.service.IAppVersionService;
import com.daisuxia.back.utils.DwzResult;
import com.daisuxia.back.utils.SpringUtils;
import com.daisuxia.web.controller.BaseController;
import com.daisuxia.web.pojo.AppVersion;
import com.daisuxia.web.pojo.BackUser;
import com.daisuxia.web.pojo.ChannelInfo;
import com.daisuxia.web.util.PageConfig;


@Controller
@RequestMapping("appVersion")
public class VersionController extends BaseController {
	
	private static Logger logger = LoggerFactory.getLogger(VersionController.class);
	@Autowired
	private IAppVersionService appVersionService;

	@RequestMapping("versionPage")
	public String versionList(HttpServletRequest request,Model model) {
		try {
		HashMap<String, Object> params = getParametersO(request);
		PageConfig<AppVersion> pageConfig = appVersionService.findPage(params);
		model.addAttribute("pm", pageConfig);
		model.addAttribute("params", params);// 用于搜索框保留值
		} catch (Exception e) {
			logger.error("get versionPage error", e);
		}
		return "version/versionPage";
	}
	/**
	 * to添加或修改页面
	 * 
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping("toPublishOrUpdate")
	public String saveUpdateChannelInfo(HttpServletRequest request,
			HttpServletResponse response, Model model, ChannelInfo channelInfo) {
		HashMap<String, Object> params = this.getParametersO(request);
		model.addAttribute("params", params);
		if(params.containsKey("id")) {
			AppVersion appVersion = appVersionService.queryObject(Integer.parseInt(params.get("id").toString()));
			model.addAttribute("appVersion", appVersion);
		}
		return "version/editVersion";
	}
	
	@RequestMapping("saveUpdateVersion")
	public void saveUpdateVersion(HttpServletRequest request,
			HttpServletResponse response, Model model, AppVersion appVersion) {
		HashMap<String, Object> params = this.getParametersO(request);
		model.addAttribute("params", params);
		// 更新或者添加操作
		String erroMsg = null;
		try {
			Date now = new Date();
			BackUser backUser = loginAdminUser(request);
			if(null==backUser) {
				erroMsg="登录已过期，请重新登录";
				SpringUtils.renderDwzResult(response, false, erroMsg,DwzResult.CALLBACK_CLOSECURRENT, null);
				return;
			}
			if (appVersion.getId() == null) {
				appVersion.setCreateTime(now);
				appVersion.setPublishTime(now);
				appVersion.setOperateName(backUser.getUserName());
				appVersionService.save(appVersion);
			} else {
				appVersion.setUpdateTime(now);
				appVersion.setOperateName(backUser.getUserName());
				appVersionService.update(appVersion);
			}
			erroMsg="操作成功";
			SpringUtils.renderDwzResult(response, true, erroMsg,DwzResult.CALLBACK_CLOSECURRENT, params.get("parentId")	.toString());
		}catch (Exception e) {
			erroMsg = "服务器异常，请稍后重试！";
			if (e.getLocalizedMessage().indexOf("UK_os_version") >= 0) {
				erroMsg = "app操作系统版本号重复！";
			}
			SpringUtils.renderDwzResult(response, false, erroMsg,DwzResult.CALLBACK_CLOSECURRENT, null);
			logger.error("添加或修改版本号失败，错误信息:"+e);
		}
	}
}
